/*******************************************************************************
 * Copyright 2011 Google Inc. All Rights Reserved.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/
package com.hiep.connect.server;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.hiep.connect.client.MyUserService;
import com.hiep.connect.server.dao.MyUserDao;
import com.hiep.connect.shared.MyUser;
import com.hiep.connect.shared.Posting;
import com.hiep.connect.shared.PostingThread;

public class MyUserServiceImpl extends RemoteServiceServlet implements
		MyUserService {

	private static final long serialVersionUID = 201204231L;

	private MyUserDao userDao;

	public MyUserServiceImpl() {
		userDao = new MyUserDao();
	}

	@Override
	public MyUser login(String email, String password)
			throws IllegalArgumentException {
		// TODO Auto-generated method stub
		MyUser currentUser = new MyUser();
		currentUser.setEmail(email);
		currentUser.setPassword(password);

		MyUser find = userDao.find(currentUser);

		MyUser generatedUser = fillUser(find);

		return generatedUser;
	}

	@Override
	public MyUser login(MyUser user) throws IllegalArgumentException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public MyUser login(String requestUri) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void logout(MyUser user) throws IllegalArgumentException {
		// TODO Auto-generated method stub
		userDao.save(user);

	}

	@Override
	public MyUser register(String email, String password)
			throws IllegalArgumentException {
		// TODO Auto-generated method stub
		MyUser newUser = new MyUser();
		newUser.setEmail(email);
		newUser.setPassword(password);

		// userDao.save(newUser);
		// MyUser find = userDao.find(newUser);

		MyUser generatedUser = fillUser(newUser);
		userDao.save(generatedUser);
		MyUser find = userDao.find(generatedUser);

		return find;
	}

	private MyUser fillUser(MyUser user) {
		List<MyUser> friends = new ArrayList<MyUser>();

		List<Posting> postings = new ArrayList<Posting>();

		for (int i = 1; i <= 10; i++) {
			// Add friends.
			MyUser u = new MyUser();
			u.setEmail("friend " + i);
			friends.add(u);

			// Create a posting thread.
			Posting p = new Posting("Posting content" + i);
			postings.add(p);
		}

		PostingThread thread = new PostingThread(postings);
		List<PostingThread> postingThreads = new ArrayList<PostingThread>();
		postingThreads.add(thread);

		user.setFriends(friends);
		user.setPostingThreads(postingThreads);

		return user;
	}
}
